package com.cdp.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.*;
import com.cdp.common.utils.StringUtils;
import com.cdp.system.dto.CementDTO;
import com.cdp.system.dto.RawDTO;
import com.cdp.system.mapper.*;
import com.cdp.system.service.ISysCementService;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.SysCementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SysCementServiceImpl extends ServiceImpl<SysCementMapper, SysCement> implements ISysCementService {

    @Autowired
    private SysCementMapper sysCementMapper;

    @Autowired
    private SysSandMapper sandMapper;

    @Autowired
    private SysStoneMapper stoneMapper;

    @Autowired
    private SysWaterReducerMapper waterReducerMapper;

    @Autowired
    private SysFlyashMapper flyashMapper;
    @Autowired
    private  SysWaterReducerMapper sysWaterReducerMapper;


    @Autowired
    private   SysMineralPowderMapper sysMineralPowderMapper;

    @Autowired
    private  SysOtherAdmixturesMapper sysOtherAdmixturesMapper;

    @Override
    public Map<String, Object> listRawData(RawDTO rawDTO) {
        Map<String, Object> result = new HashMap<>();
        List<SysCement> cementList = sysCementMapper.selectCement(rawDTO);
        // 其他表数据查询
        List<SysSand> sandList = sandMapper.selectSand(rawDTO);
        List<SysStone> stoneList = stoneMapper.selectStone(rawDTO);
        List<SysWaterReducer> sysWaterReducerList = waterReducerMapper.selectsWaterReducer(rawDTO);
        List<SysFlyash> flyashList = flyashMapper.selectFlyash(rawDTO);
        // 新加
        //水
        List<SysWater> wateList =  sysWaterReducerMapper.selectWater(rawDTO);
        //矿粉
        List<SysMineralPowder> sysMineralPowders = sysMineralPowderMapper.selectMineral(rawDTO);
        //其他参合料
        List<SysOtherAdmixtures> sysOtherAdmixturesMapperList =  sysOtherAdmixturesMapper.selectOtherAdmixtures(rawDTO);

        result.put("cement", cementList);
        result.put("sand", sandList);
        result.put("stone", stoneList);
        result.put("waterReducerList", sysWaterReducerList);
        result.put("flyash", flyashList);
        result.put("water", wateList);
        result.put("mineral", sysMineralPowders);
        result.put("otherAdmixtures", sysOtherAdmixturesMapperList);









        return result;
    }



@Override
public Map<String, Object> listPullDown() {
    // 初始化结果 Map
    Map<String, Object> resultMap = new HashMap<>();

    // 砂
    List<SysSand> sysSands = fetchSysSandList();
    Set<String> typeSet = extractUniqueField(sysSands, SysSand::getType);
    Set<String> grainSizeSet = extractUniqueField(sysSands, SysSand::getGrainSize);
    Map<String, Object> sandMap = new HashMap<>();
    sandMap.put("type", new ArrayList<>(typeSet)); // 转换为 List
    sandMap.put("grainsize", new ArrayList<>(grainSizeSet));

    // 石头
    List<SysStone> sysStones = fetchSysStoneList();
    Set<String> particleSizeDistributionSet = extractUniqueFieldForStone(sysStones, SysStone::getParticleSizeDistribution);
    Map<String, Object> stoneMap = new HashMap<>();
    stoneMap.put("particlesizedistribution", new ArrayList<>(particleSizeDistributionSet));

    // 减水剂
    List<SysWaterReducer> sysWaterReducers = fetchSysWaterReducerList();
    Set<String> modelSet = extractUniqueFieldForReducer(sysWaterReducers, SysWaterReducer::getModel);
    Map<String, Object> reducerMap = new HashMap<>();
    reducerMap.put("model", new ArrayList<>(modelSet));

    // 粉煤灰
    List<SysFlyash> sysFlyashes = fetchSysFlyashList();
    Set<String> gradeSet = extractFlyashUniqueField(sysFlyashes, SysFlyash::getGrade);
    // 显式转换 Double -> BigDecimal
    Set<BigDecimal> finenessSet = sysFlyashes.stream()
            .map(SysFlyash::getFineness) // 提取 Double 值
            .filter(Objects::nonNull) // 过滤 null 值
            //.map(BigDecimal::valueOf) // 转换为 BigDecimal
            .collect(Collectors.toSet());

    Map<String, Object> flyashMap = new HashMap<>();
    flyashMap.put("grade", new ArrayList<>(gradeSet));
    flyashMap.put("fineness", new ArrayList<>(finenessSet));

    // 合并所有数据
    Map<String, Object> dataMap = new HashMap<>();
    dataMap.put("sand", sandMap);
    dataMap.put("stone", stoneMap);
    dataMap.put("waterReducer", reducerMap);
    dataMap.put("flyash", flyashMap);

    return dataMap;
}

    /**
     * 查询水泥表的最大值最小值
     * @return
     */
    @Override
    public Map<String, Object> getCementMinAndMax() {
        // 查询水泥表的最大值和最小值
        Map<String, Object> result = sysCementMapper.queryCementMinAndMax();

        return result;
    }



    /**
     * 从数据库查询 SysSand 列表
     */
    private List<SysSand> fetchSysSandList() {
        LambdaQueryWrapper<SysSand> wrapper = new LambdaQueryWrapper<>();
        return sandMapper.selectList(wrapper);
    }

    /**
     * 从数据库查询 SysStone 列表
     */
    private List<SysStone> fetchSysStoneList() {
        LambdaQueryWrapper<SysStone> wrapper = new LambdaQueryWrapper<>();
        return stoneMapper.selectList(wrapper);
    }

    /**
     * 从数据库查询 SysWaterReducer 列表
     */
    private List<SysWaterReducer> fetchSysWaterReducerList() {
        LambdaQueryWrapper<SysWaterReducer> wrapper = new LambdaQueryWrapper<>();
        return waterReducerMapper.selectList(wrapper);
    }

    /**
     * 从数据库查询 SysFlyash 列表
     */
    private List<SysFlyash> fetchSysFlyashList() {
        LambdaQueryWrapper<SysFlyash> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(SysFlyash::getGrade, SysFlyash::getFineness)
                .isNotNull(SysFlyash::getGrade)
                .isNotNull(SysFlyash::getFineness);
        return flyashMapper.selectList(wrapper);
    }

    /**
     * 提取 SysSand 的唯一字段值
     */
    private static Set<String> extractUniqueField(List<SysSand> sysSands, Function<SysSand, String> fieldExtractor) {
        return sysSands.stream()
                .map(fieldExtractor) // 提取字段值
                .filter(Objects::nonNull) // 过滤 null 值
                .collect(Collectors.toSet()); // 去重并收集为 Set
    }

    /**
     * 提取 SysStone 的唯一字段值
     */
    private static Set<String> extractUniqueFieldForStone(List<SysStone> sysStones, Function<SysStone, String> fieldExtractor) {
        return sysStones.stream()
                .map(fieldExtractor) // 提取字段值
                .filter(Objects::nonNull) // 过滤 null 值
                .map(String::trim) // 去除空格，确保真正唯一
                .collect(Collectors.toSet()); // 去重并收集为 Set
    }

    /**
     * 提取 SysWaterReducer 的唯一字段值
     */
    private static Set<String> extractUniqueFieldForReducer(List<SysWaterReducer> sysWaterReducers, Function<SysWaterReducer, String> fieldExtractor) {
        return sysWaterReducers.stream()
                .map(fieldExtractor) // 提取字段值
                .filter(Objects::nonNull) // 过滤 null 值
                .map(String::trim) // 去除空格，确保真正唯一
                .collect(Collectors.toSet()); // 去重并收集为 Set
    }

    /**
     * 提取 SysFlyash 的唯一字段值
     */
    private static <R> Set<R> extractFlyashUniqueField(List<SysFlyash> sysFlyashes, Function<SysFlyash, R> fieldExtractor) {
        return sysFlyashes.stream()
                .map(fieldExtractor) // 提取字段值
                .filter(Objects::nonNull) // 过滤 null 值
                .collect(Collectors.toSet()); // 去重并收集为 Set
    }


    /**
     * 水泥模糊查询
     * @param
     * @return
     */

    @Override
    public AjaxResult listMistedCement(CementDTO cementDTO) {
     List<SysCementVO> sysCementList=   sysCementMapper.listMistedCement(cementDTO);
        return AjaxResult.success(sysCementList);
    }

    @Override
    public List<SysCement> listCement() {
        List<SysCement> list = list();
        return  list;
    }

    /**
     * 水泥id模糊查询
     * @param id
     * @return
     */
    @Override
    public List<IdsVO> queryCemenIdsById(Integer id) {
        LambdaQueryWrapper<SysCement> wrapper = new LambdaQueryWrapper<>();


        // 当 id 不为空时，添加模糊查询条件
        if (id != null) {
            wrapper.like(SysCement::getCementId, id);
        }

        List<SysCement> sysCementList = sysCementMapper.selectList(wrapper);
        IdsVO idsVO = new IdsVO();
        List<Integer> collect = sysCementList.stream()
                .map(SysCement::getCementId)
                .collect(Collectors.toList());

        idsVO.setIds(collect);
        return Collections.singletonList(idsVO);
    }


    /**
     * 根据Ids查询水泥信息
     * @param cementIds
     * @return
     */
    @Override
    public List<SysCement> listCementByIds(List<Integer> cementIds) {
        if (cementIds == null || cementIds.isEmpty()) {
            return Collections.emptyList();
        }

// 使用自定义查询方法获取包含地区名称的水泥信息
        return sysCementMapper.selectCementWithRegion(cementIds);
    }



}
